#include <stdio.h>
#include <string.h>
#include <netdb.h>
#include <arpa/inet.h>
void print_host(struct hostent* host);
int main(void)
{
	printf("----------gethostent start--------\n");
	struct hostent* host;
	while((host = gethostent()) != NULL)
	{
		print_host(host);
	}
	endhostent();
	printf("----------gethostent  stop--------\n");
	
	printf("\n\n----------gethostentbyname start--------\n");

	if((host = gethostbyname("jinblog.com")) != NULL)
	{
		print_host(host);
	}

	printf("----------gethostentbyname  stop--------\n");
	return 0;
}
void print_host(struct hostent* host)
{
	printf("host : %s\n",host->h_name);
	int i = 0;
	while(host->h_aliases[i] != NULL)
	{
		printf("\talias : %s\n",host->h_aliases[i]);
		i++;
	}
	switch(host->h_addrtype)
	{
		case AF_INET:
			printf("address type : AF_INET\n");
			break;
		case AF_INET6:
			printf("address type : AF_INET6\n");
			break;
		case AF_UNIX:
			printf("address type : AF_UNIX\n");
			break;
		default:
			printf("address type : unknown\n");
			break;
	}
	printf("length : %d\n",host->h_length);
	i = 0;
	char ip[32] = {'\0'};
	while(host->h_addr_list[i] != NULL)
	{
		inet_ntop(host->h_addrtype,host->h_addr_list[i],ip,sizeof(ip));
		printf("\tip : %s",ip);
		i++;
		memset(ip,'\0',sizeof(ip));
	}
	printf("\n\n\n");
}
